package com.aa.base

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import android.util.DisplayMetrics
import android.view.WindowManager
import java.io.Serializable
import java.lang.reflect.Method
import kotlin.math.abs

/**
 * @author Afra55
 * @date 4/28/21
 * A smile is the best business card.
 */
object BaseUtils {
    @JvmStatic
    fun openUrl(context: Context, url:String) {
        try {
            val intent = Intent(Intent.ACTION_VIEW);

            val uri = Uri.parse(url);

            intent.data = uri;

            context.startActivity(intent)
        } catch (e: Exception) {
        }

    }
    fun getFirstInstallTime(context: Context) :Long{
        try {
            return context.packageManager.getPackageInfo(context.packageName, 0).firstInstallTime
        } catch (e: Exception) {
            return PlayerCheck.getFirstOpenTimeNotNull()
        }
    }
    @JvmStatic
    fun getScreenWidthAndHeight(context: Context):Pair<Int, Int> {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val dm = DisplayMetrics()
        wm.defaultDisplay.getMetrics(dm)
        return dm.widthPixels to dm.heightPixels
    }
    @JvmStatic
    fun getVersionNameToVersionCode(context:Context): Pair<String, String> {
        var  versionCode = ""
        var versionName = ""
        try {
            val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
            versionName = packageInfo.versionName
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                versionCode =
                    packageInfo.longVersionCode.toString()
            } else {
                versionCode =
                    packageInfo.versionCode.toString()
            }
        }catch (e: java.lang.Exception){

        }
        return versionName to versionCode
    }
    @JvmStatic
    fun isNetworkAvailable(context: Context): Boolean {
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        @SuppressLint("MissingPermission") val activeNetworkInfo =
            connectivityManager.activeNetworkInfo
        return activeNetworkInfo?.isAvailable ?: false
    }

    fun isHarmonyOS(): Boolean {
        try {
            val clz = Class.forName("com.huawei.system.BuildEx")
            val method: Method = clz.getMethod("getOsBrand")
            return "harmony" == method.invoke(clz)
        } catch (e: java.lang.Exception) {
        }
        return false
    }
}

fun Float.dp2px():Int {
    val scale = PlayerCheck.context?.resources?.displayMetrics?.density?:1f
    return (this * scale + 0.5f).toInt()
}

fun String.absHash():Int {
    return abs(this.hashCode())
}

fun <T: AActivity> Context.startAActivity(clazz: Class<T>, vararg pas: Pair<String, Any>) {
    val intent = Intent(this, clazz)
    for (i in pas) {
        val key = i.first
        val value = i.second
        if (key.isEmpty()) {
            continue
        }
        when (value) {
            is String -> {
                intent.putExtra(key, value)
            }
            is Long -> {
                intent.putExtra(key, value)
            }
            is Double -> {
                intent.putExtra(key, value)
            }
            is Float -> {
                intent.putExtra(key, value)
            }
            is Int -> {
                intent.putExtra(key, value)
            }
            is Short -> {
                intent.putExtra(key, value)
            }
            is Byte -> {
                intent.putExtra(key, value)
            }
            is Char -> {
                intent.putExtra(key, value)
            }
            is Boolean -> {
                intent.putExtra(key, value)
            }
            is Bundle -> {
                intent.putExtra(key, value)
            }
            is Serializable -> {
                intent.putExtra(key, value)
            }
            is Parcelable ->{
                intent.putExtra(key, value)

            }
        }
    }
    startActivity(intent)
}